/*
 * 对象转换
 */
package com.business.commons.utils;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ObjectUtil {

    public static void mainx(String[] args) {

    }

    /**
     * 对象转换成map 
     *
     * @author limt
     * @param obj
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public Map<String, String> objToMap(Object obj) throws IllegalArgumentException, IllegalAccessException {
        Map<String, String> createMap = new HashMap<String, String>();
        if (null == obj) {
            return createMap;
        }
        // 得到类对象
        Class userCla = (Class) obj.getClass();
        /*
		 * 得到类中的所有属性集合
         */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            if ("serialVersionUID".equals(f.getName())) {
                continue;
            }
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = f.get(obj);// 得到此属性的值
            if (null != val) {
                createMap.put(f.getName(), val.toString());
            }
        }
        return createMap;
    }

    /**
     *
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public static String objToXml(Object req) throws IllegalArgumentException, IllegalAccessException {
        String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROWSET><ROW num=\"1\">";
        if (null == req) {
            return result.concat("</ROW></ROWSET>");
        }
        // 得到类对象
        Class userCla = (Class) req.getClass();
        /*
		 * 得到类中的所有属性集合
         */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            if ("serialVersionUID".equals(f.getName())) {
                continue;
            }
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = f.get(req);// 得到此属性的值
            //日期判断
            if (val instanceof Date) {
                val = toDateYMDHMS((Date) val);
            }
            if (null != val) {
                result = result.concat("<" + f.getName() + ">" + val.toString() + "</" + f.getName() + ">");
            }
        }
        result = result.concat("</ROW></ROWSET>");
        return result;
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String toDateYMDHMS(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String toDateYMD(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    /**
     * @param tagName 标签
     * @param att 属性
     * @param texts 内容
     * @return xml
     */
    public static String getXml(String tagName, String att, String... texts) {
        if (att == null) {
            att = "";
        } else {
            att = " " + att;
        }
        if (texts == null || texts.length == 0) {
            return "<" + tagName + att + "/>";
        } else {
            StringBuilder xml = new StringBuilder();
            xml.append("<").append(tagName).append(att).append(">");
            for (String text : texts) {
                if (text == null) {
                    continue;
                }
                xml.append(text);
            }
            xml.append("</").append(tagName).append(">");
            return xml.toString();
        }
    }

    /**
     * 替换非法字符
     *
     * @param value 要替换的String
     * @return 替换后的String
     */
    public static String replayXml(String value) {
        if (value == null) {
            return null;
        }
        value = value.replaceAll("&", "&amp;");
        value = value.replaceAll("<", "&lt;");
        value = value.replaceAll(">", "&gt;");
        value = value.replaceAll("'", "&apos;");
        value = value.replaceAll("\"", "&quot;");
        return value;
    }
}
